﻿using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using ImageAppViewModels;

namespace WpfImageDuplicates
{
    /// <summary>
    /// Interaction logic for ViewSimilarities.xaml
    /// </summary>
    public partial class ViewSimilarities : UserControl
    {
        public static readonly DependencyProperty FileImageViewModelProperty =
            DependencyProperty.Register("FileImageViewModel", typeof(FileImageViewModel), typeof(ViewSimilarities), new PropertyMetadata(null, OnFileImageViewModelPropertyChanged));

        public FileImageViewModel FileImageViewModel
        {
            get { return (FileImageViewModel)GetValue(FileImageViewModelProperty); }
            set { SetValue(FileImageViewModelProperty, value); }
        }

        private static void OnFileImageViewModelPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ViewSimilarities source = d as ViewSimilarities;
            source.UpdateFileImageViewModel(e.OldValue as FileImageViewModel, e.NewValue as FileImageViewModel);
        }

        private void UpdateFileImageViewModel(FileImageViewModel viewModelOld, FileImageViewModel viewModelNew)
        {
            if (viewModelNew != viewModelOld)
            {
                FileImageViewModel = viewModelNew;

                if (viewModelOld != null)
                {
                    viewModelOld.PropertyChanged -= viewModelNew_PropertyChanged;
                }

                viewModelNew.PropertyChanged += new PropertyChangedEventHandler(viewModelNew_PropertyChanged);
                // Let the UI bind to the view-model.
                this.DataContext = FileImageViewModel;
            }
        }

        public void UpdateComboOptions()
        {
            var entries = FileImageViewModel.SimilarityComparisonFiles;
            cboEntries.ItemsSource = entries;
            cboEntries.DisplayMemberPath = "FileNameOnly";
            UpdateSelected();             
        }

        public void UpdateSelected()
        {
            bool found = false;

            var entries = cboEntries.ItemsSource as List<SimilarityComparisonFiles>;

            if (entries != null)
            {
                for (int n = 0; n < entries.Count; n++)
                {
                    if (entries[n].FullFileName == FileImageViewModel.CurrentOther)
                    {
                        cboEntries.SelectedIndex = n;
                        found = true;
                        break;
                    }
                }

                if (!found)
                    cboEntries.SelectedIndex = 0;
            }
        }

        void viewModelNew_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "CurrentOther")
            {
                UpdateSelected();
            }
            else if (e.PropertyName == "SimilarityComparisonFiles")
            {
                UpdateComboOptions();
            }
        }

        public ViewSimilarities()
        {
            InitializeComponent();
        }

        private void cboEntries_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var sel = cboEntries.SelectedItem as SimilarityComparisonFiles;

            if (sel != null)
            {
                FileImageViewModel.CurrentOther = sel.FullFileName;
            }
        }
    }
}
